import Vue from 'vue'
import VueRouter from 'vue-router'
import { getCookie } from '@/utils'

Vue.use(VueRouter)

// 菜单路由
export const menuList = [
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/My'),
    // 路由的自定义属性
    meta: {
      title: '用户信息'
    }
  },
  {
    path: '/list',
    name: 'list',
    component: () => import('../views/List'),
    meta: {
      title: '列表'
    }
  }
]

const routes = [
  {
    path: '/',
    name: 'layout',
    component: () => import('../layout.vue'), // 整体的项目框架
    children: menuList
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由拦截器
router.beforeEach((to, from, next) => {

  if (!getCookie() && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }


})

export default router
